package com.mida.bms.report.service.impl;

import com.mida.bms.report.entity.DocStructReport;
import com.mida.bms.report.entity.DocStructTemplateInfo;
import com.mida.bms.report.mapper.DocStructReportMapper;
import com.mida.bms.report.mapper.DocStructTemplateInfoMapper;
import com.mida.bms.report.service.DocFastWriteService;
import com.mida.bms.report.vo.HistoryReportVo;
import com.mida.bms.report.vo.PatientInfoParameterVo;
import com.mida.tool.common.result.BaseResult;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数字患者
 */
@Service
@Slf4j
@Transactional
public class DocFastWriteServiceImpl extends BaseResult implements DocFastWriteService {

    @Autowired
    private DocStructTemplateInfoMapper docStructTemplateInfoMapper;

    @Autowired
    private DocStructReportMapper docStructReportMapper;

    @Override
    public ListResult getDocStructTemplateList(PatientInfoParameterVo vo) throws Exception {
        ListResult listResult = new ListResult();
        Map<String, Object> map = new HashMap<String, Object>();
        List<DocStructTemplateInfo> list = new ArrayList<DocStructTemplateInfo>();
        map.put("examServiceTypeSoid", vo.getExamServiceTypeSoid());
        map.put("doctorSoid",vo.getDoctorSoid());
        list = docStructTemplateInfoMapper.getDocStructTemplateList(map);
        listResult.setData(list);
        listResult.setStatus(HttpStatus.OK.value());
        listResult.setMessage("success");
        return listResult;
    }

    /**
     * 历史报告列表
     * @param vo
     * @return
     * @throws Exception
     */
    @Override
    public ListResult getHistoryReportList(PatientInfoParameterVo vo) throws Exception {
        ListResult listResult = new ListResult();
        List<DocStructReport> listDocStructReport = new ArrayList<DocStructReport>();
        List<HistoryReportVo> listHistoryReportVo = new ArrayList<HistoryReportVo>();
        HistoryReportVo historyReportVo = new HistoryReportVo();
        Map<String, Object> map = new HashMap<String, Object>();
        List<Long> listExamType = new ArrayList<Long>();
        listExamType.add(Long.valueOf(vo.getExamServiceTypeSoid()));
        map.put("listExamType", listExamType);
        map.put("apntPersonSoid", vo.getPersonSoid());

        if (vo.getReportProcessStatus() != null) {
            map.put("reportProcessStatus",vo.getReportProcessStatus());
        }

        if (vo.getStatus() != null) {
            map.put("status",vo.getStatus());
        }

        listDocStructReport = docStructReportMapper.getHistoryReportList(map);

        if (listDocStructReport != null && listDocStructReport.size() >0) {
            for (DocStructReport d : listDocStructReport) {
                historyReportVo = new HistoryReportVo();
                historyReportVo.setStReportSoid(d.getStReportSoid());
                historyReportVo.setRequestSoid(d.getRequestSoid());
                if (d.getUpdateDate() != null ) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    historyReportVo.setExamineDate(sdf.format(d.getUpdateDate()));
                }
                historyReportVo.setExamineContent(d.getStTemplaetName());
                listHistoryReportVo.add(historyReportVo);
            }
        }
        
        listResult.setData(listHistoryReportVo);
        listResult.setStatus(HttpStatus.OK.value());
        listResult.setMessage("success");
        return listResult;
    }

    /**
     * 历史报告列表
     * @param map
     * @return
     * @throws Exception
     */
    @Override
    public List<DocStructReport> gainHistoryReportList(
            Map<String, Object> map) throws Exception {
        return docStructReportMapper.gainHistoryReportList(map);
    }
}
